package com.aaa.behind.controller;


import com.aaa.behind.service.IBBusinessInfoService;
import com.aaa.behind.service.IEEmpInfoService;
import com.aaa.behind.service.IEExamineService;
import com.aaa.entity.BBusinessInfo;
import com.aaa.entity.EEmpInfo;
import com.aaa.entity.EExamine;
import com.aaa.util.PageInfo;
import com.aaa.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 商家信息表 前端控制器
 * </p>
 *
 * @author whj
 * @since 2023-01-05
 */
@RestController
@RequestMapping("/b-business-info")
public class BBusinessInfoController {


    @Autowired
    private IBBusinessInfoService businessInfoService;

    @Autowired
    private IEExamineService examineService;

    @Autowired
    private IEEmpInfoService empInfoService;

    @PostMapping
    public Result findAll(@ModelAttribute PageInfo page, @ModelAttribute BBusinessInfo bBusinessInfo){
        return new Result(businessInfoService.selectAll(page, bBusinessInfo));
    }

    @PostMapping("/loadingstatus")
    public Result loadingStatus(BBusinessInfo businessInfo,@ModelAttribute("telephone") String telephone){
        String SortId = businessInfoService.selectSortId(businessInfo.getSortid());
        businessInfo.setSortid(SortId);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(now);
        businessInfo.setEnterdate(format);
        EEmpInfo eEmpInfo = empInfoService.selectName(telephone);
        businessInfo.setChecker(eEmpInfo.getEname());
        saveExamine(businessInfo,eEmpInfo);
        return new Result(businessInfoService.updateById(businessInfo));
    }

    @GetMapping
    public Result findSortid(){
        return new Result(businessInfoService.findSortid());
    }


    @GetMapping("selectBid")
    public Result selectBid(){
        return new Result(businessInfoService.list());
    }


    public final boolean saveExamine(BBusinessInfo businessInfo, EEmpInfo eEmpInfo){
        EExamine eExamine = new EExamine();
        eExamine.setBid(businessInfo.getId());
        eExamine.setCheckman(eEmpInfo.getId());
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(now);
        eExamine.setGmtCreate(format);
        eExamine.setMsg(businessInfo.getReason());
        eExamine.setState(businessInfo.getStatus());
        eExamine.setIsDeleted(0);
        return examineService.saveOrUpdate(eExamine);
    }
}

